package Utils.Gameplay;

import java.util.HashMap;
import java.util.Map;

import datatypes.Datatypes;

/**
 * represents a Trivia question
 * 
 * @author Micha
 * 
 */
public class Question {
	private String questionText;
	private Map<String, Boolean> answers;

	public Question(String questionText) {
		this.setQuestionText(questionText);
		answers = new HashMap<String, Boolean>();
	}

	public Question(QueryResponse qResponse) {

		answers = new HashMap<String, Boolean>();

		setQuestionText(BuildQuestionText(qResponse));

		int counter = 0 ;
		for (Answer answer : qResponse.getAnswers().keySet()) {
			this.addAnswer(answer.getAnswer(),
					qResponse.getAnswers().get(answer));
			
			counter++;
		}
	 
	}

	private String BuildQuestionText(QueryResponse qResponse) {

		StringBuilder sb = new StringBuilder();

		sb.append(qResponse.getContextType().getConnectionRep(
				qResponse.getResultType(), qResponse.getContextInfo()));

		return sb.toString();
	}

	public String getQuestionText() {
		return questionText;
	}

	public void setQuestionText(String questionText) {
		this.questionText = questionText;
	}

	public Map<String, Boolean> getAnswers() {
		return answers;
	}

	public void setAnswers(Map<String, Boolean> answers) {
		this.answers = answers;
	}

	public void addAnswer(String answerText, boolean answerValue) {
		answers.put(answerText, new Boolean(answerValue));
	}

	public boolean isAnswerCorrect(String answerText) {
		return this.answers.get(answerText);
	}

	@Override
	public String toString() {
		StringBuilder qStr = new StringBuilder();
		qStr.append(this.questionText); 
		qStr.append("\nAnswers:\n");
		
		for(String answer : this.answers.keySet()){
			qStr.append(answer + "\t");
			qStr.append(answers.get(answer) + "\n");
		}
		
		return qStr.toString();
	}

}
